import re
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://192.168.0.173:8088/login")
    page.get_by_placeholder("请输入用户名").click()
    page.get_by_placeholder("请输入用户名").fill("cross")
    page.get_by_placeholder("请输入密码").click()
    page.get_by_placeholder("请输入密码").fill("abcd1234")
    page.get_by_role("button", name="登录").click()
    expect(page.get_by_role("button", name='前往使用')).to_be_visible()
    page.get_by_role("button", name="前往使用").click()
    expect(page.get_by_role('heading', name='跨链网关列表')).to_be_visible()

    headers = page.locator("table thead tr th")
    headers_text = [header.text_content() for header in headers.all()]
    print("表头:", headers_text)

    rows = page.locator("table tbody tr")
    if rows.count() == 0:
        print("列表中没有数据")

    # 获取第一行数据
    first_row = rows.nth(0)
    cells = first_row.locator("td")
    cells_text = [cell.text_content() for cell in cells.all()]
    print("第一行数据:", cells_text)

    # 提取网关名称
    gateway_name_index = headers_text.index("网关名称")
    gateway_name = cells_text[gateway_name_index]
    print("第一个数据的网关名称:", gateway_name)
    # ---------------------
    # 获取第二行数据
    first_row = rows.nth(1)
    cells = first_row.locator("td")
    cells_text = [cell.text_content() for cell in cells.all()]
    print("第二行数据:", cells_text)

    # 提取网关名称
    gateway_name_index = headers_text.index("网关名称")
    gateway_name = cells_text[gateway_name_index]
    print("第二个数据的网关名称:", gateway_name)
    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
